#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start Z-Wave JS service for Z-Wave radio
# ==============================================================================

# Get config values
DEVICE=$(bashio::config 'device')
SOCKET=$(bashio::config 'socket')

# Filter out null values
if [[ "${DEVICE}" == "null" ]]; then
    DEVICE=""
fi
if [[ "${SOCKET}" == "null" ]]; then
    SOCKET=""
fi

# Determine which to use
if bashio::var.has_value "${DEVICE}"; then
    CONNECTION_TYPE="device"
    SERIAL_DEVICE="${DEVICE}"
elif bashio::var.has_value "${SOCKET}"; then
    CONNECTION_TYPE="socket"
    SERIAL_DEVICE="${SOCKET}"
elif bashio::config.true 'emulate_hardware'; then
    CONNECTION_TYPE="mock"
    SERIAL_DEVICE="--mock-driver"
else
    bashio::exit.nok "Either 'device' or 'socket' must be configured"
fi

bashio::log.info "Using ${CONNECTION_TYPE}: ${SERIAL_DEVICE}"

# Send out discovery information to Home Assistant
./discovery &

# Set external config directory
export ZWAVEJS_EXTERNAL_CONFIG=/data/db

# Run daemon, passing external config directory in as environment variable
# shellcheck disable=SC2086
exec zwave-server ${SERIAL_DEVICE} --config /etc/zwave_config.json --disable-dns-sd
